package org.wendu.wdoa.service.impl;

import org.wendu.wdoa.dao.HomeDao;
import org.wendu.wdoa.dto.MenuDto;
import org.wendu.wdoa.exception.BusinessException;
import org.wendu.wdoa.service.HomeService;
import org.wendu.wdoa.model.Fun;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HomeServiceImpl implements HomeService {

    @Resource
    private HomeDao homeDao;

    @Override
    public List<MenuDto> getMenuList(String userId) {
        List<Fun> funList = homeDao.findUserFunList(userId);

        List<MenuDto> mainList = new ArrayList<>();  //主菜单集合

        MenuDto currentMainMenu = null;  //当前主菜单
        for (Fun fun: funList){
            if (currentMainMenu == null || !fun.getP_id().equals(currentMainMenu.getMenuId())){
                /*
                 * 如果当前主菜单不存在或者当前遍历的数据中的父菜单编号与当前主菜单编号不一致
                 * 应当新创建一个当前主菜单并放入主菜单集合
                 */
                currentMainMenu = new MenuDto();//内部类不能访问外部的局部变量
                currentMainMenu.setMenuId(fun.getP_id()); // 设置主菜单编号
                currentMainMenu.setMenuName(fun.getP_name());  //设置主菜单名称
                currentMainMenu.setChildren(new ArrayList<MenuDto>()); // 设置子菜单集合
                mainList.add(currentMainMenu);
            }
            //当前遍历数据中的子菜单部分分离出来封装到MenuDto对象中，并将该对象放入当前主菜单的子菜单集合中
            MenuDto subMenu = new MenuDto();
            subMenu.setMenuId(fun.getF_id());
            subMenu.setMenuName(fun.getF_name());
            subMenu.setMenuUrl(fun.getF_url());
            currentMainMenu.getChildren().add(subMenu);


        }

        return mainList;
//        funList.forEach(fun->{
//            if (currentMainMenu == null || !fun.getP_id().equals(currentMainMenu.getMenuId())){
//                /*
//                 * 如果当前主菜单不存在或者当前遍历的数据中的父菜单编号与当前主菜单编号不一致
//                 * 应当新创建一个当前主菜单并放入主菜单集合
//                 */
//                currentMainMenu = new MenuDto();//内部类不能访问外部的局部变量
//
//
//            }
//        });


    }

    @Override
    public void deleteUserById(String userId) {
        homeDao.deleteUserId(userId);
    }



    @Override
    public void findEmpName(String userId) {
        String userName = homeDao.findEmpNameById(userId);
        if (userName!=null){

            int i = checkUser(userId);
            if (i == 1){

                throw new BusinessException("该用户已存在");

            }else {
                //不存在的话创建此用户
            creatUser(userId,userName);
            return;
        }
        }
        throw new BusinessException("查无此人");
    }

    @Override
    public void creatUser(String userId, String userName) {

        homeDao.insertUser(userId,userName);

    }

    @Override
    public int checkUser(String userId) {
        int i = homeDao.checkUser(userId);
        return i;
    }

    @Override
    public int findDepIdByUserId(String userId) {
        int d_id = homeDao.findDepId(userId);
        return d_id;
    }

    @Override
    public int checkDep(int d_id) {
        int i = homeDao.checkDepp(d_id);
        return i;
    }

    @Override
    public void insertMgr(int mgrId,String userId, int d_id) {
        homeDao.insertMgrr(mgrId,userId,d_id);
    }

    @Override
    public int findMixMgId() {
        int mixMgId = homeDao.findMixMgId();
        return mixMgId;
    }

    @Override
    public void deleteMgrByDepId(int d_id) {
        homeDao.deleteMgr(d_id);
    }


}
